var api = require('../../utils/api.js');


Page({

    /**
     * 页面的初始数据
     */
    data: {
        kuaidi:'',
        desc:'',
        imgs:[],
        id:''
    },


       // 拍摄
       scanCodeEvents: function () {
        var that = this;
        wx.scanCode({
            onlyFromCamera: true, // 只允许从相机扫码
            success(res) {
                JSON.stringify(res)
                console.log(res.result)
                that.setData({
                    kuaidi:res.result
                })
            }
        })
    },


    deleteme(){
        var that=this;
        var id=this.data.id;

        wx.showModal({
            title: '删除',
            content: '确定删除?',
            complete: (res) => {
              if (res.confirm) {
                api.post({
                    url: 'kuaidi/deletesme',
                    data: {id:id},
                    success: data => {
            
                        wx.navigateBack();
                      
                    }
                })
          
  
              }
            }
          })

    },

    deleteimg(ret){
        console.log(ret);
        var i=ret.currentTarget.dataset.i;
        var imgs=this.data.imgs;
        var that=this;

        wx.showModal({
          title: '删除',
          content: '确定删除?',
          complete: (res) => {
          
            if (res.confirm) {
                var news=[];

                imgs.forEach(function(item,index){
                    if(index!=i){
                        news.push(item)
        
                    }
                })
        
                that.setData({
                    imgs:news
                })
            }
          }
        })

   

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
      var id=options.id;
      var that=this;
      if(id>0){
      
  
        api.post({
          url: 'kuaidi/getinfobyid',
          data: {id:id},
          success: data => {
  
            console.log(data);
            that.setData({
              kuaidi:data.data.order,
              imgs:data.data.img,
              desc:data.data.desc,
              id:data.data.id
            })
            
          }
      })

      }

    },
    getimgs(){
        let that = this;
        var usrl=api.HOST;

        var imgs=this.data.imgs;

        wx.chooseMedia({
          count: 1,
          sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
          sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
          success: function (res) {
            console.log(res)
      
            // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
            let tempFilePaths = res.tempFiles;
         
            wx.uploadFile({
              filePath: tempFilePaths[0].tempFilePath,
              name: 'file',
              url: usrl+'/api/common/upload',
              formData:{},
              success:function(res){
                var data= JSON.parse(res.data);

                imgs.push(data.data.img);

                that.setData({
                    imgs:imgs
                });
        
              },
              fail: function (res) {
                console.log('上传失败');
                }
            })
          }
        })
    },
    getkuaidi(ret){
        this.setData({
            kuaidi:ret.detail.value
        })
    },
    getdesc(ret){
        this.setData({
            desc:ret.detail.value
        })
    },
    submits(){
      var that=this;
      var info={}
      info.order=this.data.kuaidi;
      info.img=this.data.imgs;
      info.desc=this.data.desc;
      info.id=this.data.id;

      api.post({
        url: 'kuaidi/applykuaidi',
        data: info,
        success: data => {

          console.log(data);

            wx.showToast({
              title: data.msg,
              icon:'none'
            })
        }
    })


    }

})